namespace AbleCommerce.Admin.Shipping.Providers.FedEx
{
    using System;
    using System.Collections.Generic;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using AblePlugins.FedEx;
    using AblePlugins.FedEx.RateService9;
    using CommerceBuilder.Shipping;
    using CommerceBuilder.Utility;

    partial class Configure : CommerceBuilder.UI.AbleCommerceAdminPage
    {

        private int _ShipGatewayId = 0;
        private ShipGateway _ShipGateway;

        int ShipGatewayId
        {
            get
            {
                if (_ShipGatewayId.Equals(0))
                {
                    _ShipGatewayId = AlwaysConvert.ToInt(Request.QueryString["ShipGatewayId"]);
                }
                return _ShipGatewayId;
            }
        }

        ShipGateway ShipGateway
        {
            get
            {
                if ((_ShipGateway == null))
                {
                    _ShipGateway = ShipGatewayDataSource.Load(ShipGatewayId);
                }
                return _ShipGateway;
            }
        }

        protected string ClassId
        {
            get
            {
                return Misc.GetClassId(typeof(FedExProvider));
            }
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            // MAKE SURE A VALID SHIPGATEWAY ID IS FOUND
            if ((ShipGateway == null))
            {
                RedirectToManageProvider();
            }
            // GET THE PROVIDER INSTANCE AND MAKE SURE IT IS ACTIVE
            FedExProvider provider = (FedExProvider)ShipGateway.GetProviderInstance();
            if ((provider == null))
            {
                // THIS GATEWAY DOES NOT HAVE A FedEx PROVIDER INSTANCE (UNEXPECTED)
                // Response.Redirect("Default.aspx")
                RedirectToManageProvider();
            }

            // INITIALIZE THE PROVIDER SHIP METHOD CONTROL
            ShipMethods1.ShipGatewayId = ShipGateway.Id;

            if (!Page.IsPostBack)
            {
                // INITIALIZE THE FORM FIELDS
                UseDebugMode.SelectedValue = (provider.UseDebugMode ? "1" : "0");
                UseTestMode.SelectedValue = (provider.UseTestMode ? "1" : "0");
                EnablePackageBreakup.SelectedValue = (provider.EnablePackageBreakup ? "1" : "0");
                IncludeDeclaredValue.SelectedValue = (provider.IncludeDeclaredValue ? "1" : "0");
                FedExKey.Text = provider.Key;
                FedExPassword.Text = provider.Password;
                AccountNameDisplay.Text = provider.AccountNumber;
                MeterNumberDisplay.Text = provider.MeterNumber;
                InstanceName.Text = _ShipGateway.Name;
                BindDropOffType(provider.DropoffType);
                BindPackagingType(provider.PackagingType);
                MaxPackageWeight.Text = provider.MaxPackageWeight.ToString();
                MinPackageWeight.Text = provider.MinPackageWeight.ToString();
                TrackingURL.Text = provider.TrackingUrl;

                //DropOffType.SelectedIndex

                //CHECK WHETHER ANY PROVIDERS HAVE BEEN CONFIGURED
                IList<ShipGateway> configuredProviders = ShipGatewayDataSource.LoadForClassId(ShipGateway.ClassId);
                if (configuredProviders.Count > 1)
                {
                    trInstanceName.Visible = true;
                }
            }

            IntegrationVersion.Text = provider.Version;
        }

        protected void Page_PreRender(object sender, System.EventArgs e)
        {
            TestModeMessage.Visible = UseTestMode.SelectedValue == "1";
        }

        protected void RedirectToManageProvider()
        {
            FedExProvider provider;
            provider = new FedExProvider();
            Response.Redirect(("../Default.aspx?ProviderClassID=" + HttpUtility.UrlEncode(Misc.GetClassId(provider.GetType()))));
        }

        protected void CancelButton_Click(object sender, System.EventArgs e)
        {
            RedirectToManageProvider();
        }

        protected void SaveButton_Click(object sender, System.EventArgs e)
        {
            FedExProvider provider = (FedExProvider)ShipGateway.GetProviderInstance();
            provider.UseDebugMode = (UseDebugMode.SelectedValue == "1");
            provider.UseTestMode = (UseTestMode.SelectedValue == "1");
            provider.EnablePackageBreakup = (EnablePackageBreakup.SelectedValue == "1");
            provider.IncludeDeclaredValue = (IncludeDeclaredValue.SelectedValue == "1");
            provider.Key = FedExKey.Text;
            provider.Password = FedExPassword.Text;
            provider.AccountNumber = AccountNameDisplay.Text;
            provider.MeterNumber = MeterNumberDisplay.Text;
            provider.DropoffType = (DropoffType)AlwaysConvert.ToEnum(typeof(DropoffType), DropOffType.SelectedValue, DropoffType.REGULAR_PICKUP);
            provider.PackagingType = (PackagingType)AlwaysConvert.ToEnum(typeof(PackagingType), PackagingType.SelectedValue, AblePlugins.FedEx.RateService9.PackagingType.YOUR_PACKAGING);
            provider.MaxPackageWeight = AlwaysConvert.ToDecimal(MaxPackageWeight.Text, (decimal)provider.MaxPackageWeight);
            provider.MinPackageWeight = AlwaysConvert.ToDecimal(MinPackageWeight.Text, (decimal)provider.MinPackageWeight);
            provider.TrackingUrl = TrackingURL.Text;
            ShipGateway.UpdateConfigData(provider.GetConfigData());
            ShipGateway.Name = InstanceName.Text;
            ShipGateway.Save();

            SavedMessage.Text = string.Format(SavedMessage.Text, LocaleHelper.LocalNow);
            SavedMessage.Visible = true;
        }

        public void SaveAndCloseButton_Click(object sender, System.EventArgs e)
        {
            SaveButton_Click(sender, e);
            Response.Redirect("../Default.aspx");
        }

        protected void DeleteButton_Click(object sender, System.EventArgs e)
        {
            ShipGateway.Delete();
            Response.Redirect("../Default.aspx");
        }

        protected void BindDropOffType(DropoffType dType)
        {
            DropOffType.Items.Clear();
            foreach (DropoffType dOffType in Enum.GetValues(typeof(DropoffType)))
            {
                ListItem newItem = new ListItem(dOffType.ToString(), dOffType.ToString());
                DropOffType.Items.Add(newItem);
                if (dOffType == dType)
                {
                    newItem.Selected = true;
                }
            }
        }

        protected void BindPackagingType(PackagingType pType)
        {
            PackagingType.Items.Clear();
            foreach (PackagingType pkgType in Enum.GetValues(typeof(PackagingType)))
            {
                ListItem newItem = new ListItem(pkgType.ToString(), pkgType.ToString());
                PackagingType.Items.Add(newItem);
                if (pkgType == pType)
                {
                    newItem.Selected = true;
                }
            }
        }
    }
}